package org.suyafei.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.suyafei.enums.PluginTypeEnum;
import org.suyafei.manager.TrafficManager;
import org.suyafei.mapper.TrafficMapper;
import org.suyafei.model.TrafficDO;
import org.suyafei.utils.TimeUtil;

import java.util.Date;
import java.util.List;

/**
 * @author 宿某人
 * @Date: 2022/03/11/15:48
 * @Description:
 */
@Slf4j
@Component
public class TrafficManagerImpl implements TrafficManager {

    @Autowired
    private TrafficMapper trafficMapper;
    @Override

    public int add(TrafficDO trafficDO) {

        return trafficMapper.insert(trafficDO);

    }

    @Override
    public IPage<TrafficDO> pageAvailable(int page, int size, Long accountNo) {
        Page<TrafficDO> pageInfo = new Page<>(page, size);
        String today = TimeUtil.format(new Date(), "yyyy-MM-dd");

        trafficMapper.selectPage(pageInfo, new QueryWrapper<TrafficDO>().eq("account_no", accountNo).ge("expired_date", today).orderByDesc("gmt_create"));
        return null;
    }

    @Override
    public TrafficDO findByIdAndAccountNo(Long trafficId, Long accountNo) {
        return trafficMapper.selectOne(new QueryWrapper<TrafficDO>().eq("account_no", accountNo).eq("id", trafficId));
    }

    @Override
    public int addDayUseTimes(Long currentTrafficId, Long accountNo, int dayUseTime) {
        return trafficMapper.update(null, new UpdateWrapper<TrafficDO>().eq("account_no", accountNo).eq("id", currentTrafficId).set("day_used", dayUseTime));
    }

    @Override
    public boolean deleteExpiredTraffic() {
        int rows = trafficMapper.delete(new QueryWrapper<TrafficDO>().le("expired_date", new Date()));
        log.info("删除过期流量包row:{}",rows);
        return true;
    }

    /**
     * 查找未过期流量包列表
     * @param accountNo
     * @return
     */
    @Override
    public List<TrafficDO> selectAvailableTraffics(Long accountNo) {
        String today = TimeUtil.format(new Date(), "yyyy-MM-dd");
        QueryWrapper<TrafficDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_no", accountNo);
        queryWrapper.eq("plugin_type", PluginTypeEnum.SHORT_LINK.name());
        queryWrapper.and(wrapper -> wrapper.ge("expired_date", today).or().eq("out_trade_no", "free_init"));
        return trafficMapper.selectList(queryWrapper);
    }

    /**
     * 增加流量包使用次数
     * @param accountNo
     * @param trafficId
     * @param usedTimes
     * @return
     */
    @Override
    public int addDayUsedTimes(Long accountNo, Long trafficId, Integer usedTimes) {
        return trafficMapper.addDayUsedTimes(accountNo, trafficId, usedTimes);
    }

    /**
     * 恢复流量包使用次数
     * @param accountNo
     * @param trafficId
     * @param useTimes
     * @return
     */
    @Override
    public int releaseUsedTimes(Long accountNo, Long trafficId, Integer useTimes,String useDateStr) {
        return trafficMapper.releaseUsedTimes(accountNo, trafficId, useTimes,useDateStr);
    }

    @Override
    public int batchUpdateUsedTimes(Long accountNo, List<Long> unUpdatedTrafficIds) {

        return trafficMapper.update(null, new UpdateWrapper<TrafficDO>()
                .eq("account_no", accountNo)
                .in("id", unUpdatedTrafficIds)
                .set("day_used", 0));
    }

}
